package com.xysd.express.activity.login;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.xysd.express.R;
import com.xysd.express.base.BaseActivity;
import com.xysd.express.eventbus.RegisterSuccessEvent;
import com.xysd.express.util.ActivityTaskManeger;
import com.xysd.express.util.JSONParser;
import com.xysd.express.util.LoadingDialogUtils;
import com.xysd.express.util.StringUtils;
import com.xysd.express.util.ToastUtil;
import com.xysd.express.util.http.HttpCallBack;
import com.xysd.express.util.http.HttpUtil;

import org.greenrobot.eventbus.EventBus;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.Timer;
import java.util.TimerTask;

@ContentView(R.layout.activity_register)
public class RegisterActivity extends BaseActivity implements View.OnClickListener {

    @ViewInject(R.id.tel_et)
    private EditText tel_et;
    @ViewInject(R.id.vertifyCode_et)
    private EditText vertifyCode_et;
    @ViewInject(R.id.pwd_et)
    private EditText pwd_et;
    @ViewInject(R.id.getCode_tv)
    private TextView getCode_tv;
    @ViewInject(R.id.register_btn)
    private Button register_btn;

    private String tel,vertifyCode,pwd;

    @Override
    protected void init() {
        getCode_tv.setOnClickListener(this);
        register_btn.setOnClickListener(this);

        setTitleBar("手机快速注册");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.getCode_tv:
                getVertifyCode();
                break;
            case R.id.register_btn:
                getParams();
                rigister();
                break;
        }
    }

    private void rigister() {
        if (!StringUtils.isMobileNO(tel)){
            ToastUtil.show("请输入正确的手机号码");
            return;
        }
        if (pwd.length()<6){
            ToastUtil.show("密码不能小于6位");
            return;
        }
        if (TextUtils.isEmpty(vertifyCode)){
            ToastUtil.show("验证码不能为空");
            return;
        }
        LoadingDialogUtils.createLoadingDialog(mActivity).show();
        HttpUtil.register(tel, pwd, vertifyCode, new HttpCallBack() {
            @Override
            public void onSuccess(String result) {
                LoadingDialogUtils.closeLoadingDialog();
                Log.i("注册",result);
                String code=JSONParser.getStringFromJsonString("Status",result);
                String msg=JSONParser.getStringFromJsonString("Msg",result);
                if ("0".equals(code)){
                    EventBus.getDefault().post(new RegisterSuccessEvent(tel,pwd));
                    ActivityTaskManeger.getInstance().closeActivity(mActivity);
                }else {
                    ToastUtil.show(msg);
                }
            }

            @Override
            public void onFailure(String error) {
                LoadingDialogUtils.closeLoadingDialog();
                Log.e("注册",error);
            }
        });
    }

    private void getVertifyCode() {
        getCode_tv.getLayoutParams().width=getCode_tv.getWidth();
        tel=tel_et.getText().toString();
        if (!StringUtils.isMobileNO(tel)){
            ToastUtil.show("请输入正确的手机号码");
            return;
        }
        HttpUtil.getVertifyCodeRegister(tel, new HttpCallBack() {
            @Override
            public void onSuccess(String result) {
                Log.i("注册验证码",result);
                String code= JSONParser.getStringFromJsonString("Status",result);
                String msg= JSONParser.getStringFromJsonString("Msg",result);
                if ("0".equals(code)){
                    countdown(60);
                    ToastUtil.show("获取验证码成功");
                }else {
                    ToastUtil.show(msg);
                }
            }

            @Override
            public void onFailure(String error) {
                Log.e("注册验证码",error);
            }
        });

    }

    private void getParams() {
        tel=tel_et.getText().toString();
        pwd=pwd_et.getText().toString();
        vertifyCode=vertifyCode_et.getText().toString();
    }


    int recLen=60;
    Timer timer;
    //倒计时
    private void countdown(final int time) {
        recLen=time;
        timer=new Timer();
        timer.schedule(new TimerTask() {//因为timer和timertask均只可调用一次，所以写在内部不用全局变量
            @Override
            public void run() {

                runOnUiThread(new Runnable() {      // UI thread
                    @Override
                    public void run() {
                        recLen--;
                        getCode_tv.setText(""+recLen);
                        if(recLen < 0){
                            timer.cancel();
                            getCode_tv.setText("重新获取");
                            recLen = time;
                            getCode_tv.setClickable(true);
                        }
                    }
                });
            }
        },1000,1000);//第二参数为延迟，第三参数为执行task的run方法间隔时间，即多久变一次，单位均为毫秒
        getCode_tv.setClickable(false);
    }
}
